package com.nimt.hqgl_idea.service;


import com.nimt.hqgl_idea.entity.Order;
import com.nimt.hqgl_idea.util.ResponseBody;
import com.nimt.hqgl_idea.mapper.OrderMapper;
import com.nimt.hqgl_idea.param.OrderParam;
import com.nimt.hqgl_idea.util.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Override
    public ResponseBody add(Order order) {
        orderMapper.insert(order);
        return new ResponseBody(order);
    }

    @Override
    public ResponseBody query(OrderParam orderParam) {
        ResponseBody responseBody = null;
        orderParam.setTotalCount(orderMapper.count(orderParam.getUserId(),orderParam.getKeywords()));
        int start = (orderParam.getCurrentPage()-1)*orderParam.getPageSize();
        List<Order> orders = orderMapper.selectAll(start, orderParam.getPageSize(), orderParam.getUserId(),orderParam.getKeywords());
        if(orders==null || orders.size() == 0){
            responseBody = new ResponseBody(ErrorEnum.No_More_Data);
        }else {
            orderParam.setData(orders);
            responseBody = new ResponseBody(orderParam);
        }
        return responseBody;
    }

    @Override
    public ResponseBody state(Integer id) {
        orderMapper.updateState(id);
        return new ResponseBody("订单接收成功!");
    }

    @Override
    public ResponseBody del(Integer id) {
        orderMapper.delete(id);
        return new ResponseBody("删除成功!");
    }
}
